//
//  DealDetail.m
//  BrushFace
//
//  Created by timer_q on 16/1/27.
//  Copyright © 2016年 Zzz. All rights reserved.
//

#import "DealDetail.h"
#import "DealInfoCell.h"
#import "DealInfo.h"

@interface DealDetail ()<UITableViewDataSource,UITableViewDelegate>
{
    UITableView *_tableView;
    NSMutableArray *_dataArray;
    NSInteger _cnt;
}
@end

@implementation DealDetail

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self UIConfig];
    [self.navBar setTitle:@"交易明细"];
    [self.navBar setLeftBtn:NAV_BAR_LEFT_BTN_TYPE_BLUE];
    self.view.backgroundColor = UIColorFromRGB(ColorValueForBackView_Gray);
    _dataArray = [[NSMutableArray alloc] init];
    _cnt = 0;
    NSString *requestStr = [NSString stringWithFormat:@"%@/user/listtradlogs.json",SERVICE_ADRESS];
    NSDictionary *params = @{@"curPage":@"0",
                             @"rPP":@"10"};
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
        NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
        [_dataArray addObjectsFromArray:jsondict[@"data"]];
        [_tableView reloadData];
    } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
        
    }];
    [self setRefresh];
}

#pragma mark - 上拉下拉
- (void)setRefresh{
    @weakify (self);
    [_tableView addPullToRefreshWithActionHandler:^{
        @strongify (self);
        NSString *requestStr = [NSString stringWithFormat:@"%@/user/listtradlogs.json",SERVICE_ADRESS];
        NSDictionary *params = @{@"curPage":@"0",
                                 @"rPP":@"10"};
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            [self->_dataArray removeAllObjects];
            [self->_dataArray addObjectsFromArray:jsondict[@"data"]];
            [self->_tableView reloadData];
            [self->_tableView endAllRefreshingWithEnd:NO];
        } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            
        }];    }];
    [_tableView addInfiniteScrollingWithActionHandler:^{
        @strongify (self);
        NSString *requestStr = [NSString stringWithFormat:@"%@/user/listtradlogs.json",SERVICE_ADRESS];
        NSDictionary *params = @{@"curPage":[NSString stringWithFormat:@"%ld",_cnt+=1],
                                 @"rPP":@"10"};
        AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
        [manager GET:requestStr parameters:params success:^(AFHTTPRequestOperation * _Nonnull operation, id  _Nonnull responseObject) {
            NSDictionary *jsondict = [NSJSONSerialization JSONObjectWithData:operation.responseData options:0 error:nil];
            [self->_dataArray addObjectsFromArray:jsondict[@"data"]];
            [self->_tableView reloadData];
            if ([jsondict[@"data"] count]<10) {
                [self->_tableView endAllRefreshingWithEnd:YES];
            }else{
                [self->_tableView endAllRefreshingWithEnd:NO];
            }
        } failure:^(AFHTTPRequestOperation * _Nullable operation, NSError * _Nonnull error) {
            
        }];
       
    }];
    
}

- (void)UIConfig{
    _tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 65, SCREEN_WIDTH, SCREEN_HEIGHT-65) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    _tableView.tableFooterView = [[UIView alloc] init];
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark -tableviewDelegate 
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return _dataArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    DealInfoCell *cell = [DealInfoCell loadBundleNib];
    NSDictionary *dict = [_dataArray objectAtIndex:indexPath.row];
    cell.cellTitle.text = dict[@"optype"];
    if ([dict[@"optype"] isEqualToString:@"还款"]) {
        cell.cellImage.image = [UIImage imageNamed:@"还"];
    }
    if ([dict[@"optype"] isEqualToString:@"充值"]) {
        cell.cellImage.image = [UIImage imageNamed:@"充"];
    }
    if ([dict[@"optype"] isEqualToString:@"借入"]) {
        cell.cellImage.image = [UIImage imageNamed:@"入"];
    }
    if ([dict[@"optype"] isEqualToString:@"借出"]) {
        cell.cellImage.image = [UIImage imageNamed:@"出"];
    }
    if ([dict[@"optype"] isEqualToString:@"回款"]) {
        cell.cellImage.image = [UIImage imageNamed:@"回"];
    }
    if ([dict[@"optype"] isEqualToString:@"提现"]) {
        cell.cellImage.image = [UIImage imageNamed:@"提"];
    }
    cell.cellMoney.text = [NSString stringWithFormat:@"%@",dict[@"amount"]];
    cell.cellResult.text = [NSString stringWithFormat:@"%@",dict[@"status"]];
    cell.cellDate.text = [NSString stringWithFormat:@"%@",dict[@"tradetime"]];
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 81;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    NSDictionary *dict = [_dataArray objectAtIndex:indexPath.row];
    DealInfo *dvc = [[DealInfo alloc] init];
    if ([dict[@"optype"] isEqualToString:@"还款"]) {
        dvc.type = @"0";
    }
    if ([dict[@"optype"] isEqualToString:@"充值"]) {
        dvc.type = @"1";
    }
    if ([dict[@"optype"] isEqualToString:@"借入"]) {
        dvc.type = @"2";
    }
    if ([dict[@"optype"] isEqualToString:@"借出"]) {
        dvc.type = @"3";
    }
    if ([dict[@"optype"] isEqualToString:@"回款"]) {
        dvc.type = @"4";
    }
    if ([dict[@"optype"] isEqualToString:@"提现"]) {
        dvc.type = @"5";
    }
    dvc.stateStr = dict[@"status"];
    dvc.infoid = dict[@"id"];
    [dvc setHidesBottomBarWhenPushed:YES];
    [self.navigationController pushViewController:dvc animated:YES];
}

@end
